home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / stik_dev / forecast / stikstuf.c < prev    next >
C/C++ Source or Header  |  1995-12-28  |  4KB  |  173 lines

  1. #include <aes.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <osbind.h>
  7.  
  8. #include "main.h"
  9. #include "drivers.h"
  10. #include "transprt.h"
  11.  
  12. #define CON 2
  13.  
  14. int init_network(void);
  15. int open_socket(char *, int);
  16. int close_socket(int);
  17. int read_from_socket(int, char *, int);
  18. int send_to_socket(int, char *, int);
  19.  
  20. static long init_drivers(void);
  21.  
  22. extern WINDOW win;
  23. extern void draw_win(void);
  24.  
  25. char out[80];
  26.  
  27.  
  28. /* These definitions are necessary.  transprt.h has external
  29.  * declarations for them.
  30.  */
  31. DRV_LIST *drivers = (DRV_LIST *)NULL;
  32. TPL *tpl = (TPL *)NULL;
  33.  
  34. /* Put 'STIK' cookie value into drivers */
  35.  
  36. typedef struct {
  37.     long cktag;
  38.     long ckvalue;
  39. } ck_entry;
  40.  
  41. long init_drivers(void)
  42. {
  43.     long i = 0;
  44.     ck_entry *jar = *((ck_entry **) 0x5a0);
  45.  
  46.     while (jar[i].cktag) {
  47.         if (!strncmp((char *)&jar[i].cktag, CJTAG, 4)) {
  48.             drivers = (DRV_LIST *)jar[i].ckvalue;
  49.             return (0);
  50.         }
  51.         ++i;
  52.     }
  53.     return (0);    /* Pointless return value...    */
  54. }
  55.  
  56. int init_network(void)
  57. {
  58.     static long init_drivers(void);
  59.      
  60.  
  61.     Supexec(init_drivers);
  62.  
  63.     /* See if we got a value    */
  64.  
  65.      if (drivers == (DRV_LIST *)NULL) {
  66.         strcpy(out, "STiK is not loaded");
  67.         wind_set(win.handle, WF_INFO, (short)((long)out>>16), (short)out, NULL, NULL);
  68.         return (FALSE);
  69.     }
  70.  
  71.     /* Check Magic number    */
  72.  
  73.     if (strcmp(MAGIC, drivers->magic)) {
  74.         strcpy(out, "Magic string doesn't match");
  75.         wind_set(win.handle, WF_INFO, (short)((long)out>>16), (short)(out), NULL, NULL);
  76.         return (FALSE);
  77.     }
  78.  
  79.     /* OK, now we can get the address of the "TRANSPORT" layer
  80.      * driver.  If this seems unnecessarily complicated, it's
  81.      * because I tried to create today, what I would like to
  82.      * use later on.  In future, there will be multiple
  83.      * drivers accessible via this method.  With luck, your
  84.      * code will still work with future versions of my software.
  85.      */
  86.  
  87.     tpl = (TPL *)get_dftab(TRANSPORT_DRIVER);
  88.  
  89.     if (tpl == (TPL *)NULL) {
  90.         strcpy(out, "Transport layer *not* loaded");
  91.         wind_set(win.handle, WF_INFO, (short)((long)out>>16), (short)(out), NULL, NULL);
  92.         return (FALSE);
  93.     }
  94.  
  95.     sprintf(out, "TPL %s, v%s", tpl->author, tpl->version);
  96.     wind_set(win.handle, WF_INFO, (short)((long)out>>16), (short)(out), NULL, NULL);
  97.  
  98.     return (TRUE);
  99. }
  100.  
  101. int close_socket(int cn)
  102.     {
  103.     int tstat;
  104.      
  105.  
  106.     tstat = (int)TCP_close(cn, 0);
  107.     sprintf(out, "%s", get_err_text(tstat));
  108.     wind_info(win.handle, out);
  109.     draw_win();
  110.  
  111.     return (tstat);
  112. }
  113.  
  114. int open_socket(char *hostname, int port)
  115.     {
  116.     int cn, x;
  117.     unsigned long rhost;
  118.      
  119.  
  120.     if ((x = resolve(hostname, (char **)NULL, &rhost, 1)) < 0) 
  121.         {
  122.         sprintf(out, "Open error: %s", get_err_text(x));
  123.         wind_info(win.handle, out);
  124.         draw_win();
  125.         return (-1);
  126.         }
  127.  
  128.     if ((cn = TCP_open(rhost, port, 0, 2000)) < 0)
  129.         {
  130.         sprintf(out, "TCP_open() returns %s", get_err_text(cn));
  131.         wind_info(win.handle, out);
  132.         draw_win();
  133.         return (-1);
  134.         }
  135.  
  136.     return (cn);
  137. }
  138.  
  139. int send_to_socket(int cn, char *str, int len)
  140.     {
  141.     return (TCP_send(cn, str, len));
  142.     }
  143.  
  144. int read_from_socket(int cn, char *str, int len)
  145.     {
  146.     int count, c, i = 0;
  147.     clock_t to = clock() + (clock_t)10 * CLK_TCK;
  148.  
  149.     while (TRUE)
  150.         {
  151.         count = CNbyte_count(cn);
  152.         if (count < 0)        return (count);
  153.         if (to < clock())    return (E_USERTIMEOUT);
  154.  
  155.         if (Bconstat(CON))
  156.             {
  157.             Bconin(CON);
  158.             return (E_USERTIMEOUT);
  159.             }
  160.         else 
  161.                 {
  162.                 while (count > 0)
  163.                 {
  164.                 count--;
  165.                 to = clock() + (clock_t)CLK_TCK;
  166.                 c = CNget_char(cn);
  167.                 if (c < E_NODATA)    return (c);
  168.                 else if (c>=0 && i<len)    str[i++] = (char)c;
  169.                 }
  170.             }
  171.         }
  172.     }
  173.